package request.share;

import java.io.File;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class Cnfg
{
    private static Cnfg instance=null;

    private Cnfg()
    {//{{{
        this.log=new Log();
        this.access=new Access();
        this.workers=WORKER_DEF_NUM;
    }//}}}

    public static synchronized Cnfg getInstance()
    {//{{{
        if(instance == null)
        {
            instance=new Cnfg();
        }
        return instance;
    }//}}}

    private static String __CLASS__=Cnfg.class.getName();

    public static final int WORKER_DEF_NUM=4;

    public class Log
    {//{{{
        public String location;
        public boolean tracking;
    }//}}}
    public Log log;

    public class Access
    {//{{{
        public int server_port;
        public int monitor_port;
    }//}}}
    public Access access;

    public int workers;

    public boolean load()
    {//{{{
        Document doc;
        try
        {
            SAXBuilder builder=new SAXBuilder();
            doc=builder.build(new File("server.cnfg.xml"));
        }
        catch(Exception e)
        {
            System.out.println(__CLASS__+
                    ": load server.cnfg.xml fail: "+
                    e.getMessage());
            return false;
        }

        Element root=doc.getRootElement();
        Element section;
        String section_s;
        Element property;

        section=root.getChild("log"); 
        if(section == null)
        {
            System.out.println(__CLASS__+": get "+
                    section.getName()+" fail");
            return false;
        }
        section_s="["+section.getName()+"] ";
        property=section.getChild("location");
        if(property == null)
        {
            System.out.println(__CLASS__+": get "+
                    section.getName()+"."+property.getName()+
                    " fail");
            return false;
        }
        this.log.location=property.getText();
        System.out.println(__CLASS__+": "+section_s+
                property.getName()+":"+property.getText());
        property=section.getChild("tracking");
        this.log.tracking=false;
        if(property != null)
        {
            String opt=property.getText().trim().toLowerCase();
            if(opt.equals("true") || opt.equals("yes") || opt.equals("1"))
                this.log.tracking=true;
        }
        System.out.println(__CLASS__+": "+section_s+
                property.getName()+":"+property.getText());

        section=root.getChild("access"); 
        if(section == null)
        {
            System.out.println(__CLASS__+": get "+
                    section.getName()+" fail");
            return false;
        }
        section_s="["+section.getName()+"] ";
        property=section.getChild("server_port");
        if(property == null)
        {
            System.out.println(__CLASS__+": get "+
                    section.getName()+"."+property.getName()+" fail");
            return false;
        }
        this.access.server_port=Integer.parseInt(property.getText());
        System.out.println(__CLASS__+": "+section_s+
                property.getName()+":"+property.getText());
        property=section.getChild("monitor_port");
        if(property == null)
        {
            System.out.println(__CLASS__+": get "+
                    section.getName()+"."+property.getName()+" fail");
            return false;
        }
        this.access.monitor_port=Integer.parseInt(property.getText());
        System.out.println(__CLASS__+": "+section_s+
                property.getName()+":"+property.getText());

        return true;
    }//}}}
}
